#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"

void STinit(ST* x)
{
	assert(x);
	x->a = (STDataType*)malloc(sizeof(STDataType) * 4);
	if (x->a == NULL)
	{
		perror("malloc fail");
		return;
	}
	x->capacity = 4;
	x->top = 0;
}

void STDestory(ST* x)
{
	assert(x);
	free(x->a);
	x->a = NULL;
	x->capacity = 0;
	x->top = 0;
}

void STPush(ST* x, STDataType num)
{
	assert(x);
	if (x->top == x->capacity)
	{
		STDataType* tmp = (STDataType*)realloc(x->a, sizeof(STDataType) * x->capacity * 2);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		x->a = tmp;
		x->capacity *= 2;
	}
	x->a[x->top] = num;
	x->top++;
}

void STPop(ST* x)
{
	assert(x);
	assert(!STEmpty(x));
	x->top--;
}

int STSize(ST* x)
{
	assert(x);
	return x->top;
}

bool STEmpty(ST* x)
{
	assert(x);
	return x->top == 0;
}

STDataType STTOP(ST* x)
{
	assert(x);
	assert(!STEmpty(x));
	return x->a[x->top - 1];
}
